package com.tqjc.execute.thread.aysn;

import com.tqjc.execute.consts.EleTaskLogConstans;
import com.tqjc.execute.utils.HttpPLCUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: 86195
 * @CreateTime: 2024-09-03  20:52
 * @Description: 异步电气自动任务点位日志监控
 * @Version: 1.0
 */
@Data
@Slf4j
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class AysnEleTaskLogThread extends Thread{

    //线程是否运行标识   true:继续运行  false:停止
    private Boolean flag;


    private String craneName;

    private RestTemplate restTemplate;

    private String host;


    @Override
    public void run() {
        log.info("======异步打印电气监控自动化任务点位日志线程开始,天车为：{}=======",craneName);

        long start = System.currentTimeMillis();
        while (flag){

            for (String label : EleTaskLogConstans.ELE_LOG_LABEL_LIST) {
                try {
                    String val = HttpPLCUtils.plcReadbc(restTemplate, host, craneName, label , false);
                    log.info("天车为：{}，异步打印电气监控自动化任务点位日志线程,监控点位标签为：{}，读出来的值为：{}",craneName,label,val);
                } catch (Exception e){
                    log.error("天车为：{}，异步打印电气监控自动化任务点位日志线程,出现异常",craneName);

                }
            }
            try {
                Thread.sleep(300);
            }catch (Exception e){
            }

            if(System.currentTimeMillis() - start > 120*1000){
                log.info("异步打印电气监控自动化任务点位日志线程,天车：{},时长已运行超过2分钟",craneName);
                break;
            }

        }


        log.info("======异步打印电气监控自动化任务点位日志线程结束,天车为：{}=======",craneName);


    }
}
